
/*
 * 文件名：TestServiceImpl.java.
 * 描述：
 * 创建人: 龚洪亮
 * 创建时间：2017年8月9日
 */

package com.fiberhome.platform.service;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.entity.TestCaseOut;
import com.fiberhome.platform.mapper.TestCaseMapper;
import com.fiberhome.platform.mapper.TestCategoryMapper;
import com.fiberhome.platform.pojo.TestCase;
import com.fiberhome.platform.pojo.TestCaseExample;
import com.fiberhome.platform.pojo.TestCategory;
import com.fiberhome.platform.pojo.TestCategoryExample;


/**
 * @Description 
 * @author 龚洪亮
 * @version 2017年8月9日
 */
@Service
public class TestServiceImpl implements TestService {

	@Autowired
	private TestCaseMapper testCaseMapper;
	@Autowired
	private TestCategoryMapper testCategoryMapper;
	@Autowired
	private TestCategoryMapper testCatMapper;
	@Override
	@Transactional
	public PageResult saveTest(TestCase test) {
		
		//首先将测试用例编号添加到TestCategory中
		TestCategory testCat = testCatMapper.selectByPrimaryKey(test.getId());
		testCat.setCaseNo(test.getCaseNo());
		testCatMapper.updateByPrimaryKeySelective(testCat);
		
		//插入测试用例
		//TestCase的id首先设为空，因为之前的id仅仅是用于接收前台的id参数的，并不是测试用例的实际id
		test.setId(null);
		test.setCreatedTime(new Date());
		test.setUpdatedTime(new Date());
		
		TestCategoryExample example = new TestCategoryExample();
		TestCategoryExample.Criteria criteria = example.createCriteria();
		criteria.andCaseNoEqualTo(test.getCaseNo());
		List<TestCategory> testCats = testCategoryMapper.selectByExample(example);
		List<TestCategory> parentList = testCategoryMapper.selectAllParent(testCats.get(0).getId());
		int size = parentList.size();
		
		if (size==0) {
			
		} else if (size==1) {
			test.setPonCase(parentList.get(0).getId());
		} else if (size==2) {
			test.setTestClass1(parentList.get(0).getId());
			test.setPonCase(parentList.get(1).getId());
		} else if (size==3) {
			test.setTestClass1(parentList.get(0).getId());
			test.setTestClass2(parentList.get(1).getId());
			test.setPonCase(parentList.get(2).getId());
		} else if (size==4) {
			test.setTestClass1(parentList.get(0).getId());
			test.setTestClass2(parentList.get(1).getId());
			test.setTestClass3(parentList.get(2).getId());
			test.setPonCase(parentList.get(3).getId());
		} else if (size==5) {
			test.setTestClass1(parentList.get(0).getId());
			test.setTestClass2(parentList.get(1).getId());
			test.setTestClass3(parentList.get(2).getId());
			test.setTestClass4(parentList.get(3).getId());
			test.setPonCase(parentList.get(4).getId());
		}
		if (testCaseMapper.insert(test) > 0) {
			return PageResult.ok(test);
		} else {
			return PageResult.ok();
		}
	}

	
	@Override
	public PageResult editTest(TestCase test) {
		TestCategoryExample example = new TestCategoryExample();
		TestCategoryExample.Criteria criteria = example.createCriteria();
		criteria.andCaseNoEqualTo(test.getCaseNo());
		List<TestCategory> testCats = testCategoryMapper.selectByExample(example);
		List<TestCategory> parentList = testCategoryMapper.selectAllParent(testCats.get(0).getId());
		int size = parentList.size();
		
		if (size==0) {
			
		} else if (size==1) {
			test.setPonCase(parentList.get(0).getId());
		} else if (size==2) {
			test.setTestClass1(parentList.get(0).getId());
			test.setPonCase(parentList.get(1).getId());
		} else if (size==3) {
			test.setTestClass1(parentList.get(0).getId());
			test.setTestClass2(parentList.get(1).getId());
			test.setPonCase(parentList.get(2).getId());
		} else if (size==4) {
			test.setTestClass1(parentList.get(0).getId());
			test.setTestClass2(parentList.get(1).getId());
			test.setTestClass3(parentList.get(2).getId());
			test.setPonCase(parentList.get(3).getId());
		} else if (size==5) {
			test.setTestClass1(parentList.get(0).getId());
			test.setTestClass2(parentList.get(1).getId());
			test.setTestClass3(parentList.get(2).getId());
			test.setTestClass4(parentList.get(3).getId());
			test.setPonCase(parentList.get(4).getId());
		}
		
		
		
		//TestCase pre = testCaseMapper.selectByPrimaryKey(test.getId());
		//test = (TestCase)CopyNotNull.copyTo(pre, test);
		test.setUpdatedTime(new Date());
		if (testCaseMapper.updateByPrimaryKey(test) > 0) {
			return PageResult.ok(test);
		} else {
			return PageResult.ok();
		}
	}

	@Override
	@Transactional
	public PageResult deleteTest(Integer id) {
		try {
			TestCase needCat = testCaseMapper.selectByPrimaryKey(id);
			testCaseMapper.deleteByPrimaryKey(id);
			String num = needCat.getCaseNo();
			TestCaseExample example = new TestCaseExample();
			com.fiberhome.platform.pojo.TestCaseExample.Criteria criteria = example.createCriteria();
			criteria.andCaseNoEqualTo(num);
			testCaseMapper.deleteByExample(example);
			return PageResult.ok(needCat);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return PageResult.build(500, "删除异常");
	}


	@Override
	public TestCaseOut getTestByNo(String no) {
		TestCaseExample example = new TestCaseExample();
		com.fiberhome.platform.pojo.TestCaseExample.Criteria criteria = example.createCriteria();
		criteria.andCaseNoEqualTo(no);
		List<TestCaseOut> re = testCaseMapper.selectByExample(example);
		return re.get(0);
	}


	@Override
	public PageResult isDup(String caseNo) {
		TestCaseExample example = new TestCaseExample();
		TestCaseExample.Criteria criteria = example.createCriteria();
		criteria.andCaseNoEqualTo(caseNo);
		List<TestCaseOut> result = testCaseMapper.selectByExample(example);
		if (result.size()==0) {
			return PageResult.ok();
		}
		return PageResult.ok(result.get(0));
	}

}
